//
// Created by zhupi on 2024/9/5.
//

#ifndef DEMO2_GALLERY_BASE_CELL_WIDGET_H
#define DEMO2_GALLERY_BASE_CELL_WIDGET_H

#include <QWidget>
#include <QHBoxLayout>
#include <QGridLayout>

#include <QToolButton>



#include <QWidget>
#include <QAction>
#include <QGridLayout>
#include <QToolButton>
#include <QFrame>


class PToolButton : public QToolButton
{
Q_OBJECT
public:
    explicit PToolButton(QWidget *parent = nullptr) : QToolButton(parent) {
        setStyleSheet(R"(
            QToolButton {
                background: transparent;
            }
            QToolButton:hover {
                background-color: rgba(255, 255, 255, 0.3); /* 悬停时的背景色 */
            }
            QToolButton:pressed {
                background-color: rgba(255, 255, 255, 0.5); /* 点击时的背景色 */
            }
        )");

    }
};




class IconPanel : public QWidget {
Q_OBJECT

public:
    IconPanel(QWidget *parent = nullptr) : QWidget(parent), currentColumn(0), currentRow(0) {
        layout = new QGridLayout(this);
        layout->setHorizontalSpacing(5);  // 设置适当的列间距
        layout->setVerticalSpacing(5);    // 设置适当的行间距
        this->setStyleSheet("background: red;");  // 设置背景透明
        setLayout(layout);
    }

    void AddSeparator() {
        QFrame *line = new QFrame();
        line->setFrameShape(QFrame::VLine);
        line->setFrameShadow(QFrame::Raised);
        line->setLineWidth(1);
        layout->addWidget(line, 0, currentColumn, layout->rowCount(), 1);
        currentColumn++;
    }

    // 添加弹簧
    void AddSpacer() {
//        QSpacerItem *spacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding);
//        layout->addItem(spacer, 0, currentColumn, layout->rowCount(), 1);
    }

    void FinalizeLayout() {
        AddSeparator();
        AddSpacer();
    }

    void AddLargeAction(QAction* action) {
        PToolButton* button = new PToolButton();
        button->setDefaultAction(action);
        button->setIconSize(QSize(64, 64)); // 统一宽度为64

        if(currentRow != 0) {
            currentColumn++;
            currentRow = 0;
        }
        button->setFixedSize(64, 64);  // 设置固定大小
        button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

        layout->addWidget(button, currentRow, currentColumn, 3, 1);
        currentColumn++;
        currentRow = 0;  // 每次添加大图标后，行数重置
    }

    void AddMediumAction(QAction* action) {
        PToolButton* button = new PToolButton();
        button->setDefaultAction(action);
        button->setIconSize(QSize(64, 32)); // 统一宽度为64

        button->setFixedSize(64, 32);  // 设置固定大小
        button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

        // 设置按钮的图标和文本
        button->setIcon(action->icon());
        button->setText(action->text());
        button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
        layout->addWidget(button, currentRow, currentColumn, 2, 1);
        currentRow += 2;

        if (currentRow >= 3) {
            currentColumn++;
            currentRow = 0;  // 每次达到3行后，换列
        }
    }

    void AddSmallAction(QAction* action) {
        PToolButton* button = new PToolButton();
        button->setDefaultAction(action);
        button->setIconSize(QSize(64, 20)); // 统一宽度为64
        button->setFixedSize(64, 20);  // 设置固定大小

        button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
        // 设置按钮的图标和文本
        button->setIcon(action->icon());
        button->setText(action->text());
        button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

        layout->addWidget(button, currentRow, currentColumn, 1, 1);
        currentRow++;

        if (currentRow >= 3) {
            currentColumn++;
            currentRow = 0;  // 每次达到3行后，换列
        }
    }

private:
    QGridLayout *layout;
    int currentColumn;
    int currentRow;
};




class GalleryBaseCellWidget : public QWidget
{
    Q_OBJECT
public:
    explicit GalleryBaseCellWidget(QWidget *parent);
    // 把action加入到pannel，并以大图标显示
    void AddLargeAction(QAction* action);
    // 把action加入到pannel，在三行模式下会以中图标显示
    void AddMediumAction(QAction* action);
    // 把action加入到pannel，并以小图标显示
    void AddSmallAction(QAction* action);

private:
    QHBoxLayout *mainHbox_ {nullptr};
    QGridLayout *widgetVbox_ {nullptr};

    int columnindex_ {0};  // 当前列
    int rowIndex_ {0}; // 当前行
};


#endif //DEMO2_GALLERY_BASE_CELL_WIDGET_H
